package com.leetcode.LC;

public class LC695 {
    int maxArea=0,tmp=0;
    public int maxAreaOfIsland(int[][] grid) {
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == 1) {
                    dfs(grid, i, j);
                    if (tmp>maxArea){
                        maxArea=tmp;
                    }
                    tmp=0;
                }
            }
        }
        return maxArea;
    }

    public void dfs(int[][] grid, int row, int col) {
        if (row < 0 || row >= grid.length || col < 0 || col >= grid[0].length || grid[row][col] == 0) {
            return;
        }
        grid[row][col] = 0;
        tmp++;
        dfs(grid, row, col - 1);
        dfs(grid, row, col + 1);
        dfs(grid, row - 1, col);
        dfs(grid, row + 1, col);

    }


    public static void main(String[] args) {
        System.out.println(new LC695().maxAreaOfIsland(new int[][]{
                {1, 1, 1, 1, 0},
                {1, 1, 0, 1, 0},
                {1, 1, 0, 0, 0},
                {0, 0, 0, 0, 0}
        }));
    }
}